﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Admin_DevWork_Add : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click1(object sender, EventArgs e)
    {
        string uername = User_Num_Textbox.Text.ToString();
        string password = User_Pass_Textbox.Text.ToString();
        string name = User_Name_Textbox.Text.ToString();
        string sex = User_Sex_DropDown.SelectedValue.ToString();
        string tel = User_Tel_Textbox.Text.ToString();
        string email = User_Email_Textbox.Text.ToString();
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringProManage"].ConnectionString;

        //try
        //{
        try
        {
            conn.Open();
            string SQL = "insert into Users (uername,password,name,sex,tel,email) values (@uername,@password,@name,@sex,@tel,@email)"; SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = SQL;

            //依次添加每个字段的值
            SqlParameter puername = new SqlParameter("@uername", SqlDbType.VarChar, 10);
            puername.Value = uername;
            cmd.Parameters.Add(puername);

            SqlParameter ppassword = new SqlParameter("@password", SqlDbType.VarChar, 20);
            ppassword.Value = password;
            cmd.Parameters.Add(ppassword);

            SqlParameter pname = new SqlParameter("@name", SqlDbType.NVarChar, 10);
            pname.Value = name;
            cmd.Parameters.Add(pname);

            SqlParameter psex = new SqlParameter("@sex", SqlDbType.NVarChar, 2);
            psex.Value = sex;
            cmd.Parameters.Add(psex);

            SqlParameter ptel = new SqlParameter("@tel", SqlDbType.VarChar, 11);
            ptel.Value = tel;
            cmd.Parameters.Add(ptel);

            SqlParameter pemail = new SqlParameter("@email", SqlDbType.NVarChar, 30);
            pemail.Value = email;
            cmd.Parameters.Add(pemail);

            int R = cmd.ExecuteNonQuery();  //R 的值就是返回影响表的行数
            if (R > 0)
            {
                Session["ReturnPath"] = "Admin_DevWork_Add.aspx";
                Session["Message"] = "恭喜，操作成功！";
                Response.Redirect("Success.aspx");
            }
            else
            {
                Session["Message"] = "插入失败，请检查您的操作！";
                Response.Redirect("Error.aspx");
            }

        }
        finally
        {
            conn.Close();
        }
    }
}